﻿package com.snzflash.components 
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.ui.Keyboard;
	import gs.easing.Expo;
	import gs.TweenLite;
	
	/**
	 * закладка, которая отображается при сочетании клавиш ctrl + shift + UP||DOWN
	 * @author firsoff maxim, snzflash.com, tel: +79222345364, skype: maxim.firsoff
	 */
	public class AuthorMark extends Sprite
	{
		private var _label:TextField;
		private var _shape:Shape;
		private var _color:uint = 0xFFFFFF;
		private var __visible:Boolean;
		private var _text:String = "programmer : firsov maxim, \n<a href='http://snzflash.com'>snzflash.com</a>\n<a href='mailto:firsoffmaxim@gmail.com'>firsoffmaxim@gmail.com</a>";
		public function AuthorMark() 
		{
			super();
			addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			//
			y = 5;
			_label = new TextField();
			_label.selectable = false;
			_label.autoSize = TextFieldAutoSize.LEFT;
			_label.htmlText = _text;
			
			_shape = new Shape();
			_shape.graphics.beginFill(_color, 0.6);
			_shape.graphics.drawRoundRect(0, 0, _label.width, _label.height, 5, 5);
			////trace(width, height);
			addChild(_shape);
			addChild(_label);
		}
		
		private function onAddedToStage(event:Event):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			stage.addChild(this);
			stage.addEventListener(Event.RESIZE, onStageResize);
			x = Math.round(stage.stageWidth);
			visible = false;
			onStageResize();
			// настойка клавиатуры
			stage.addEventListener (KeyboardEvent.KEY_DOWN, onKeyDown);
		};
		
		private function onKeyDown(event:KeyboardEvent):void 
		{
			if (event.ctrlKey && event.shiftKey && (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN )) 
			{
				visible = true;
			}
			else 
			{
				visible = false;
			}
		};
		
		override public function get visible():Boolean { return super.visible; };
		
		override public function set visible(value:Boolean):void 
		{
			__visible = value;
			stage.addChild(this);
			var posx:Number = value ? Math.round(stage.stageWidth - width - 5) :  Math.round(stage.stageWidth);
			TweenLite.to ( this, 0.5, { x:posx, ease : Expo.easeOut  } );
		};
		
		private function onStageResize(event:Event = null):void 
		{
			TweenLite.killTweensOf(this);
			////trace('resize');
			if (__visible){ x = Math.round(stage.stageWidth - width - 5);}
			else 
			{
				x = Math.round(stage.stageWidth);
			}
			
		};
		
	}

}